package com.ruoyi.web.controller.system;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ComplaintEncloaure;
import com.ruoyi.system.service.IComplaintEncloaureService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 投诉附件Controller
 * 
 * @author ruoyi
 * @date 2021-06-22
 */
@RestController
@RequestMapping("/system/complaint/encloaure")
public class ComplaintEncloaureController extends BaseController
{
    @Autowired
    private IComplaintEncloaureService complaintEncloaureService;

    /**
     * 查询投诉附件列表
     */
//    @PreAuthorize("@ss.hasPermi('system:complaint/encloaure:list')")
    @GetMapping("/list")
    public TableDataInfo list(ComplaintEncloaure complaintEncloaure)
    {
        startPage();
        List<ComplaintEncloaure> list = complaintEncloaureService.selectComplaintEncloaureList(complaintEncloaure);
        return getDataTable(list);
    }

    /**
     * 导出投诉附件列表
     */
//    @PreAuthorize("@ss.hasPermi('system:complaint/encloaure:export')")
    @Log(title = "投诉附件", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ComplaintEncloaure complaintEncloaure)
    {
        List<ComplaintEncloaure> list = complaintEncloaureService.selectComplaintEncloaureList(complaintEncloaure);
        ExcelUtil<ComplaintEncloaure> util = new ExcelUtil<ComplaintEncloaure>(ComplaintEncloaure.class);
        return util.exportExcel(list, "complaint/encloaure");
    }

    /**
     * 获取投诉附件详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:complaint/encloaure:query')")
    @GetMapping(value = "/{coeId}")
    public AjaxResult getInfo(@PathVariable("coeId") Long coeId)
    {
        return AjaxResult.success(complaintEncloaureService.selectComplaintEncloaureById(coeId));
    }

    /**
     * 新增投诉附件
     */
//    @PreAuthorize("@ss.hasPermi('system:complaint/encloaure:add')")
    @Log(title = "投诉附件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add( ComplaintEncloaure complaintEncloaure, MultipartFile file)
    {try {
        String profile = RuoYiConfig.getProfile();
        byte[] bytes = file.getBytes();
        //文件夹
        Path path2 = Paths.get(profile+"/cId/" + complaintEncloaure.getCcId());
        //具体文件
        Path path = Paths.get(profile+"/cId/" + complaintEncloaure.getCcId() +"/" + file.getOriginalFilename());
        File file1 = path.toFile();
        //创建文件夹
        if (!file1.exists()) {
            path2.toFile().mkdirs();
        }
        if (file1.isFile()) {
            return AjaxResult.error("文件上传失败,文件存在");
        } else {
            Files.write(path, bytes);//创建文件
            complaintEncloaure.setCoeUrl("profile/htId/" + complaintEncloaure.getCcId() + "/" + file.getOriginalFilename());
        }
    }catch (Exception e){

    }
        return toAjax(complaintEncloaureService.insertComplaintEncloaure(complaintEncloaure));
    }

    /**
     * 修改投诉附件
     */
//    @PreAuthorize("@ss.hasPermi('system:complaint/encloaure:edit')")
    @Log(title = "投诉附件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ComplaintEncloaure complaintEncloaure)
    {
        return toAjax(complaintEncloaureService.updateComplaintEncloaure(complaintEncloaure));
    }

    /**
     * 删除投诉附件
     */
//    @PreAuthorize("@ss.hasPermi('system:complaint/encloaure:remove')")
    @Log(title = "投诉附件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{coeIds}")
    public AjaxResult remove(@PathVariable Long[] coeIds)
    {
        return toAjax(complaintEncloaureService.deleteComplaintEncloaureByIds(coeIds));
    }
}
